# -*- coding: UTF-8 -*-
import requests
import json
import template
from manager import hot_news_manager
from manager import draft
from token_cache.access_token import token_instance


def get_material():
    response = requests.post("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token="
                             + token_instance.access_token,
                             json={
                                 "type": "image",
                                 "offset": 0,
                                 "count": 10
                             })
    print(response.content)


def create_article():
    content = '&nbsp;&nbsp;&nbsp;&nbsp;1月8日，' \
              '2022年国家医保谈判正式收官。据国家医保局医药管理司负责人介绍，阿兹夫定片、清肺排毒颗粒谈判成功，' \
              '辉瑞新冠药Paxlovid因生产企业辉瑞报价高未能成功。对于Paxlovid，2022年2月，国家药监局根据《药品管理法》' \
              '相关规定，按照药品特别审批程序进行应急审评审批，附条件批准该药物的进口注册。2022年3月，Paxlovid被纳入' \
              '《新型冠状病毒肺炎诊疗方案（试行第九版）》，目前已被纳入《新型冠状病毒感染诊疗方案（试行第十版）》。从附条件获' \
              '批至今，Paxlovid价格一直是行业各界以及民众所关注的焦点。辉瑞新冠药为何不愿意放低姿态？2022国家医保谈判收官：' \
              '辉瑞新冠口服药为何“灵魂砍价”失败？- 21财经'
    draft_content = template.draft_add_template.format("wxeb39b10e39bf6b54", "pages/question/index?id=577680053",
                                                       "知乎",
                                                       "2022国家医保谈判收官，辉瑞新冠口服药「灵魂砍价」失败，辉瑞新冠药为何不愿意放低姿态？",
                                                       content)
    response = requests.post("https://api.weixin.qq.com/cgi-bin/draft/add?access_token=" + access_token,
                             data=json.dumps({
                                 "articles": [
                                     {
                                         "title": "第一篇文章",
                                         "author": "王瑞峰",
                                         "digest": "第一篇文章摘要",
                                         "content": draft_content,
                                         "content_source_url": "123.57.3.130",
                                         "thumb_media_id": "UWvbZL67SdUMEtBDkWAe8uvBlbQz9uorSDqBGuF4dDHOs4ypw44pTYeMP5O3RMzt",
                                         "need_open_comment": 0,
                                         "only_fans_can_comment": 0
                                     }, {
                                         "title": "第二篇文章",
                                         "author": "王瑞峰",
                                         "digest": "第一篇文章摘要",
                                         "content": draft_content,
                                         "content_source_url": "123.57.3.130",
                                         "thumb_media_id": "UWvbZL67SdUMEtBDkWAe8uvBlbQz9uorSDqBGuF4dDHOs4ypw44pTYeMP5O3RMzt",
                                         "need_open_comment": 0,
                                         "only_fans_can_comment": 0
                                     }
                                 ]
                             }, ensure_ascii=False).encode('utf-8'), headers={"Content-Type": "application/json"})
    print(response.content)


if __name__ == '__main__':
    # get_material()
    news_list = hot_news_manager.query()
    data = {}
    for hot_news in news_list:
        if len(hot_news) > 0:
            data[hot_news[0].type_id] = hot_news
    draft.add_draft("123.57.3.130", data)
